﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

// 1. 修改自定义类名
public class FileItem
{
    public string IdentificationCode { get; set; }
    public string Encryption { get; set; }
}

// 2. 更新数据库类
public class FileDatabase
{
    public List<FileItem> FileItems { get; set; } = new List<FileItem>();
}

public static class XmlFileManager
{
    private static readonly string AppDataPath = Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
        "YourAppName",
        "FileDatabase.xml"
    );

    public static void EnsureDatabaseExists()
    {
        string directory = Path.GetDirectoryName(AppDataPath);

        if (!Directory.Exists(directory))
        {
            Directory.CreateDirectory(directory);
        }

        if (!System.IO.File.Exists(AppDataPath))
        {
            CreateEmptyDatabase();
        }
    }

    private static void CreateEmptyDatabase()
    {
        var emptyDatabase = new FileDatabase();
        SaveToXml(emptyDatabase);
    }

    public static void SaveToXml(FileDatabase data)
    {
  
        using (var stream = new FileStream(AppDataPath, FileMode.Create))
        {
            var serializer = new XmlSerializer(typeof(FileDatabase));
            serializer.Serialize(stream, data);
        }
    }

    public static FileDatabase LoadFromXml()
    {
        EnsureDatabaseExists();

        using (var stream = new FileStream(AppDataPath, FileMode.Open))
        {
            var serializer = new XmlSerializer(typeof(FileDatabase));
            return (FileDatabase)serializer.Deserialize(stream);
        }
    }

    public static void AddFile(FileItem newFile)
    {
        var database = LoadFromXml();
        database.FileItems.Add(newFile);
        SaveToXml(database);
    }
}